'''
Created on Mar 3, 2014

@author: Hovanes Egiyan
'''



class crate(object):
    '''
    Class for the crate entries in voltage database
    '''
    
    crateDict = {}
    classID = 0
    
    def __init__(self, nameArg=None, hostArg=None ):
        '''
        Initialize the crate class using input arguments
        '''
        self.crateid        = self.incrementID()
        self.name           = nameArg
        self.area           = None
        self.rack           = None
        self.location       = None
        self.SN             = None
        self.Function       = "BIAS/LV"
        self.rocid          = None
        self.host           = hostArg
        self.IP             = None  
        
        self.boardDict      = {}
                
        if( nameArg in crate.crateDict.keys() ):
            print "Crate called %s is already in the dictionary of crates, not adding"
            return
        else:
            crate.crateDict.update( { self.name: self } )    
        
        return
        

    
    def AddBiasBoard( self, slot=None ):
        '''
        Add a bias board to this crate
        '''
        newBoard = module( self, slot, "ISEG" )
        # If there is a board in this slot of this chassis, replace the board
        if( slot in self.boardDict.keys( ) ):
            print "Replacing existing board in slot %s with new BIAS board" %slot
        self.boardDict.update( { slot : newBoard } )

        return
    
    
    def AddLVBoard( self, slot=None ):
        '''
        Add a low voltage board to this crate
        '''
        newBoard = module( self, slot, "MPV8008L" )
        # If there is a board in this slot of this chassis, replace the board
        if( slot in self.boardDict.keys( ) ):
            print "Replacing existing board in slot %s with new LV board" %slot 
        
        self.boardDict.update( { slot : newBoard } )

        return
   
       
    def makeSQL(self, fileHandle):
        '''
        Create SQL string and append it into the output file
        '''
        sql_string = "INSERT INTO Crate ( crateid, name, Function, host, area, rack, location, rocid ) VALUES (%s, '%s', '%s', '%s', '%s', '%s', '%s', '%s');\n" \
            %(self.crateid, self.name, self.Function, self.host, self.area, self.rack, self.location, self.rocid )
        fileHandle.write(sql_string)

        for slotNum in self.boardDict.keys():
            self.boardDict[slotNum].makeSQL(fileHandle)
        
        return
        
        
    def incrementID(self):
        crate.classID += 1 
        return crate.classID
     

class module(object):
    '''
    Class for the module entries in voltage database
    '''
    numberOfChans = { "ISEG" : 16, "MPV8008L" : 8 }
    channelTypeMap = { "ISEG" : "bias_chanid", "MPV8008L" : "lv_chanid" }
    
    classID = 0
    
    def __init__(self, crateArg=None, slotArg=None, typeArg="ISEG" ):
        '''
        Initialize the module class using input arguments
        '''
        self.moduleid       = self.incrementID()        
        self.crateid        = crateArg.crateid
        self.slot           = slotArg
        self.type           = typeArg
        self.SN             = None

        self.channelMap     = {}            # Map of all channels on this module
        # Loop over channels and add them to the module
        for iChan in range( 0, module.numberOfChans[typeArg] ):
            newChannel = channel( self, iChan, module.channelTypeMap[typeArg] )
            self.channelMap.update( {iChan:newChannel} )
        
    def makeSQL(self, fileHandle):
        '''
        Create SQL string and append it into the output file
        '''
        sql_string = "INSERT INTO Module ( moduleid, crateid, slot, type ) VALUES (%s, %s, '%s', '%s');\n" %(self.moduleid, self.crateid, self.slot, self.type )
        
        fileHandle.write(sql_string)

        for chanNum in self.channelMap.keys() :
            self.channelMap[chanNum].makeSQL(fileHandle)
        return

    def incrementID(self):
        module.classID += 1 
        return module.classID
       


class channel(object):
    '''
    Class for the slot entries in voltage database
    '''
    classID = 0

    def __init__(self, moduleArg=None, channelArg=None, typeArg="bias_chanid", enableArg=1 ):
        '''
        Initialize the channel class using input arguments
        '''
        self.chanid         = self.incrementID()
        self.moduleid       = moduleArg.moduleid
        self.name           = None
        self.channel        = channelArg
        self.system         = None
        self.col_name       = typeArg
        self.enable         = enableArg
        
    def makeSQL(self, fileHandle):
        '''
        Create SQL string and append it into the output file
        '''
        sql_string = "INSERT INTO Channel ( chanid, moduleid, channel, col_name) VALUES (%s, %s, %s, '%s');\n" %(self.chanid, self.moduleid, self.channel, self.col_name)
        
        fileHandle.write(sql_string)
        return

    def incrementID(self):
        channel.classID += 1 
        return channel.classID

   
    